package homework.homework2;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseEveLis extends MouseAdapter implements ActionListener {

    private final UpPan up;

    private String s = "";

    public MouseEveLis(UpPan floodrPan) {
        this.up = floodrPan;
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        JButton b = (JButton) e.getSource();

        if("←".equals(b.getText())){
            String s1 = "";
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length -1; i++) {
                s1 += chars[i];
            }

            s = s1;
        }else {
            s += b.getText();
            //System.out.println(s);
            //  System.out.println(b.getText());
        }

        if("=".equals(b.getText())){

            String[] ss = s.split("(\\+)|(÷)|(x)|(-)|(=)");
           switch (s.charAt(ss[0].length())){
               case '+':
                   s = String.valueOf(Double.parseDouble(ss[0]) + Double.parseDouble(ss[1]));
                   break;
               case '-':
                   s = String.valueOf(Double.parseDouble(ss[0]) - Double.parseDouble(ss[1]));
                   break;
               case 'x':
                   s = String.valueOf(Double.parseDouble(ss[0]) * Double.parseDouble(ss[1]));
                   break;
               case '÷':
                   s = String.valueOf(Double.parseDouble(ss[0]) / Double.parseDouble(ss[1]));
                   break;
            }
        }

        if("AC".equals(b.getText())){
            s = "";
        }

        up.getJt().setText(s);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }
}
